package com.huaxindata.common.base;

import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.AppCompatImageButton;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.FrameLayout;
import android.widget.TextView;

import com.huaxindata.common.R;
import com.huaxindata.common.util.HxDimenUtil;

/**
 * @author songxf
 * @date 2017/9/20
 * @Desc 基类Activity 主要封装公共标题栏，和容器布局
 * Copyright HUAXIN DATA. All rights reserved.
 */
public class HxBaseLayoutActivity extends AppCompatActivity {
    private AppCompatImageButton mBack;
    private TextView             mTitle;
    public  TextView             mSubtitle;
    private FrameLayout          mContentLayout;
    private Toolbar              mToolbar;
    private boolean              isTitleBarShow;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);//竖屏
        super.setContentView(R.layout.activity_base_layout);
        mToolbar = (Toolbar) findViewById(R.id.toolbar);
        mBack = (AppCompatImageButton) findViewById(R.id.toolbar_back);
        mTitle = (TextView) findViewById(R.id.toolbar_title);
        mContentLayout = (FrameLayout) findViewById(R.id.layout_content);
        mSubtitle = (TextView) findViewById(R.id.toolbar_subtitle);

        //默认显示标题栏
        setShowTitleBar(true);
        //默认不显示返回按钮
        setShowBack(false);
        //默认不显示子标题
        setShowSubTitle(false);
    }

    /**
     * 设置标题栏显示或者隐藏
     *
     * @param isShow true则显示,false为不显示
     */
    public boolean setShowTitleBar(boolean isShow) {
        if (mToolbar != null) {
            mToolbar.setVisibility(isShow ? View.VISIBLE : View.GONE);
        }
        isTitleBarShow = isShow;
        return isTitleBarShow;
    }

    /**
     * 是否显示返回按钮
     *
     * @param isShow true则显示,false为不显示
     */
    public void setShowBack(boolean isShow) {
        if (!isTitleBarShow)
            return;
        if (mToolbar != null) {
            mBack.setVisibility(isShow ? View.VISIBLE : View.GONE);
            mBack.setOnClickListener(isShow ? new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    onBackPressed();
                }
            } : null);

            mTitle.setPadding(isShow ? 0 : HxDimenUtil.dip2px(this, 16), 0, 0, 0);
        }

    }

    /**
     * 是否显示子标题
     *
     * @param isShow true则显示,false为不显示
     */
    public void setShowSubTitle(boolean isShow) {
        if (!isTitleBarShow)
            return;
        if (mSubtitle != null) {
            mSubtitle.setVisibility(isShow ? View.VISIBLE : View.GONE);
        }
    }

    /**
     * 设置标题内容
     *
     * @param titleId
     */
    public void setTitle(int titleId) {
        if (!isTitleBarShow)
            return;
        mTitle.setText(titleId);
    }

    /**
     * 获取标题TextView
     *
     * @return
     */
    public TextView getTitleview() {
        if (mTitle != null) {
            return mTitle;
        }
        return null;
    }

    /**
     * 设置标题内容
     *
     * @param title
     */
    public void setTitle(CharSequence title) {
        if (!isTitleBarShow)
            return;
        mTitle.setText(title);
    }


    @Override
    public void setContentView(int layoutResID) {
        mContentLayout.removeAllViews();
        View.inflate(this, layoutResID, mContentLayout);
        onContentChanged();
    }

    @Override
    public void setContentView(View view) {
        mContentLayout.removeAllViews();
        mContentLayout.addView(view);
        onContentChanged();
    }

    @Override
    public void setContentView(View view, LayoutParams params) {
        mContentLayout.removeAllViews();
        mContentLayout.addView(view, params);
        onContentChanged();
    }
}
